﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.Odbc;
using Org.SqlHelper;

namespace QualityRpt.Dao.Db2
{
    class FunctionDao:IFunctionDao
    {
        const string cachePrefix = "FL-";
        public DataRow Save(DataRow row)
        {
            object rt = null;
            using (OdbcConnection conn = ODBCHelper.GetConnection())
            {
                ODBCHelper.ExecuteNonQuery(conn, CommandType.Text, "INSERT INTO RPT.RPT_FUNCTION(MODULE_NAME,MODULE_URI,PARENT_ID,MODULE_LEVEL,MODULE_INDEX) VALUES(?,?,?,?,?)",
                    ODBCHelper.CreateInputParameter("MODULE_NAME", OdbcType.VarChar,50, row["MODULE_NAME"].ToString()),
                    ODBCHelper.CreateInputParameter("MODULE_URI", OdbcType.VarChar,255, row["MODULE_URI"].ToString()),
                    ODBCHelper.CreateInputParameter("PARENT_ID", OdbcType.Int, Convert.ToInt32(row["CONTENT"])),
                    ODBCHelper.CreateInputParameter("MODULE_LEVEL", OdbcType.Int, Convert.ToInt32(row["MODULE_LEVEL"])),
                    ODBCHelper.CreateInputParameter("MODULE_INDEX", OdbcType.Int, Convert.ToInt32(row["MODULE_INDEX"])));
                rt = ODBCHelper.ExecuteScalar(conn, CommandType.Text, "SELECT IDENTITY_VAL_LOCAL() FROM SYSIBM.SYSDUMMY1");
            }
            CacheManager.RemoveByPrefix(cachePrefix);
            if (rt != null)
            {
                row["ID"] = Convert.ToInt32(rt);
            }
            return row;
        }

        public DataRow Update(DataRow row)
        {
            using (OdbcConnection conn = ODBCHelper.GetConnection())
            {
                ODBCHelper.ExecuteNonQuery(conn, CommandType.Text, "UPDATE RPT.RPT_FUNCTION SET MODULE_NAME=?,MODULE_URI=?,PARENT_ID=?,MODULE_LEVEL=?,MODULE_INDEX=? WHERE ID=?",
                    ODBCHelper.CreateInputParameter("MODULE_NAME", OdbcType.VarChar, 50, row["MODULE_NAME"].ToString()),
                    ODBCHelper.CreateInputParameter("MODULE_URI", OdbcType.VarChar, 255, row["MODULE_URI"].ToString()),
                    ODBCHelper.CreateInputParameter("PARENT_ID", OdbcType.Int, Convert.ToInt32(row["CONTENT"])),
                    ODBCHelper.CreateInputParameter("MODULE_LEVEL", OdbcType.Int, Convert.ToInt32(row["MODULE_LEVEL"])),
                    ODBCHelper.CreateInputParameter("MODULE_INDEX", OdbcType.Int, Convert.ToInt32(row["MODULE_INDEX"])),
                    ODBCHelper.CreateInputParameter("ID", OdbcType.Int, Convert.ToInt32(row["ID"])));
            }
            DataTable rtTable = GetList("ID=" + row["ID"].ToString());
            CacheManager.RemoveByPrefix(cachePrefix);
            if (rtTable.Rows.Count > 0)
            {
                return rtTable.Rows[0];
            }
            return null;
        }

        public DataRow Get(long id)
        {
            DataTable rtTable = GetList("ID=" + id.ToString());
            if (rtTable.Rows.Count > 0)
            {
                return rtTable.Rows[0];
            }
            return null;
        }

        public void Delete(long id)
        {
            using (OdbcConnection conn = ODBCHelper.GetConnection())
            {
                ODBCHelper.ExecuteNonQuery(conn, CommandType.Text, "DELETE FROM RPT.RPT_FUNCTION WHERE ID=?",
                    ODBCHelper.CreateInputParameter("ID", OdbcType.Int, id));
            }
            CacheManager.RemoveByPrefix(cachePrefix);
        }

        public DataTable GetList(string where)
        {
            return GetList(where, "MODULE_INDEX ASC");
        }

        public DataTable GetList(string where, string sort)
        {
            StringBuilder sbText = new StringBuilder();
            sbText.Append("SELECT * FROM RPT.RPT_FUNCTION ");
            if (where.Trim() != string.Empty)
            {
                sbText.Append(" WHERE " + where);
            }
            if (sort.Trim() != string.Empty)
            {
                sbText.Append(" ORDER BY " + sort);
            }
            DataTable rtTable = null;
            string cacheKey = cachePrefix + sbText.ToString();
            if (CacheManager.Get(cacheKey) == null)
            {
                using (OdbcConnection conn = ODBCHelper.GetConnection())
                {
                    rtTable = ODBCHelper.ReadTable(conn, CommandType.Text, sbText.ToString());
                }
                CacheManager.Put(cacheKey, rtTable);
            }
            else
            {
                rtTable = CacheManager.Get(cacheKey) as DataTable;
            }
            return rtTable;
        }

        public DataTable GetPageList(Utils.PageUtils pu, string where, string sort)
        {
            throw new NotImplementedException();
        }
    }
}
